package com.litchi.activity;

 import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.SaveListener;

import com.litchi.R;
import com.litchi.bean.UserBean;
import com.litchi.utils.NetWorkUtils;
import com.litchi.utils.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 注册模块
 * @author Administrator
 *
 */
public class SignUpActivity extends Activity implements OnClickListener {

	private EditText mEditUser;
	private EditText mEditPsw;
	private Button mBtnSignUp;
	private boolean isBtnChecked = true;
	private EditText mEditPswVal;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_up);
		 Bmob.initialize(this, "881230b0542b0d1540a1b9f77c01804a");
		mBtnSignUp = (Button) findViewById(R.id.btn_sign_up);
		mEditUser = (EditText) findViewById(R.id.edit_uid);
		mEditPsw = (EditText) findViewById(R.id.edit_psw);
		mEditPswVal = (EditText) findViewById(R.id.edit_psw_val);
		CheckBox btnCheck = (CheckBox) findViewById(R.id.btn_check);
		TextWatcher watcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				setSignable();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		};
		mEditUser.addTextChangedListener(watcher);
		mEditPsw.addTextChangedListener(watcher);
		mEditPswVal.addTextChangedListener(watcher);
		btnCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				isBtnChecked = isChecked;
				setSignable();
			}

		});
		mBtnSignUp.setOnClickListener(this);
		findViewById(R.id.img_back).setOnClickListener(this);
	}

	/**
	 * 是否符合注册条件
	 */
	private void setSignable() {
		if (isBtnChecked) {

			if (mEditUser.getText().toString().length() == 11
					&& mEditPsw.getText().toString().length() > 5
					&& mEditPswVal.getText().toString().length() > 5) {
				mBtnSignUp.setEnabled(true);
			} else {
				mBtnSignUp.setEnabled(false);
			}
		} else {
			mBtnSignUp.setEnabled(false);
		}
	}

	/**
	 * 注册
	 */
	private void signup() {
		String userName = mEditUser.getText().toString();
		String pwd = mEditPsw.getText().toString();
		String pwdVal = mEditPswVal.getText().toString();

		
		boolean isNum = userName.matches("[0-9]+");
		
		if (!isNum) {
			mEditUser.setText("");
			mEditUser.setError("请输入正确格式的手机号");
			mEditUser.setFocusable(true);
			return;
		}
		if (!pwd.equals(pwdVal)) {
			mEditPswVal.setText("");
			mEditPswVal.setError("两次输入的密码不一致");
			mEditPswVal.setFocusable(true);
			return;
		}
		UserBean user = new UserBean();
		user.setUsername(userName);
		user.setPassword(pwd);
		user.setBalance(10000);
		user.setPoints(1000);
		user.setMobilePhoneNumber(userName);
		user.signUp(this, new SaveListener() {
			public void onSuccess() {
				Toast.makeText(SignUpActivity.this, "注册成功， 请登录",
						Toast.LENGTH_LONG).show();
				finish();
			}

			public void onFailure(int arg0, String arg1) {
				mEditUser.setError("注册失败"+arg1);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			finish();
			break;
		case R.id.btn_sign_up:
			if (NetWorkUtils.isNetworkAvailabel(this)) {
				signup();
			}else {
				Utils.showToast(this, "请检查网络是否可用");
			}
			
			break;

		default:
			break;
		}
	}

}
